package com.tiyende.kash.loop.ui.presenter

import android.Manifest
import com.lib.common.permission.CommonPermissions
import com.lib.common.permission.Permission
import com.tiyende.kash.loop.common.CommonPresenter
import com.tiyende.kash.loop.configs.TiyendeApiPath
import com.tiyende.kash.loop.configs.TiyendeApiUrl
import com.tiyende.kash.loop.configs.TiyendeApiUrl.addCommonParams
import com.tiyende.kash.loop.request.IHttpCallback
import com.tiyende.kash.loop.ui.contract.IViewContract
import com.tiyende.kash.loop.utils.helper.TiyendeStatusHelper
import com.tiyende.kash.loop.utils.helper.TiyendeProcessHelper
import com.tiyende.kash.loop.utils.helper.TiyendeUserDecInvok
import com.tiyende.kash.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.kash.loop.utils.shared.TiyendeSharedKey
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/9/10 13:41
 * @Annotation：额度审核中
 */
class TiyendeLimitPresenter(private val iView: IViewContract.ITiyendeLimitReviewingView): CommonPresenter(iView) {
    /**
     * 获取授信状态
     */
    fun queryTiyendeCreditStatusAction(){
        iView.hiddenTiyendeLoading()
        val isCreditReviewing = TiyendeStatusHelper.TiyendeCreditStatusRepository().isCreditReviewing()
        //授信中
        if (isCreditReviewing){
            iView.showTiyendeLimitReviewingViews()
            return
        }

        val creditStatus = TiyendeProcessHelper.queryTiyendeAuthenticationBeforeRiskStatus()
        val isCreditRejected = TiyendeStatusHelper.TiyendeCreditStatusRepository().isCreditRejected(creditStatus)
        //授信被拒绝
        if (isCreditRejected){
            iView.showTiyendeLimitFailedViews()
            return
        }
        //授信完成
        queryTiyendeWithdrawAmountAction()
    }

    /**
     * 获取额度信息
     */
    private fun queryTiyendeWithdrawAmountAction(){
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path", TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_LOAN_LIMIT_PATH)
        }
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    iView.showTiyendeLimitSuccessViews(obj)
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }

    /**
     * 检查权限是否全部申请
     */
    fun checkTiyendeLimitReviewingPermissionsAgree(){
        //权限页操作状态 : 0未操作  1已同意  -1拒绝
        val permissionStatus = TiyendeSharedHelper.queryTiyendeSharedInt(TiyendeSharedKey.TIYENDE_PERMISSION_PAGE_STATUS)
        //权限列表未操作 -> 进入权限列表
        if (permissionStatus == 0){
            iView.showTiyendeLimitPermissionPage()
            return
        }

        //是否是广告渠道
        val isAdSource = TiyendeSharedHelper.queryTiyendeSharedBoolean(TiyendeSharedKey.TIYENDE_IS_ADSOURCE)
        //广告渠道 && 权限列表未同意 -> 进入权限列表
        if (isAdSource && permissionStatus == -1){
            iView.showTiyendeLimitPermissionPage()
            return
        }
        //广告渠道 && 未同意sms权限 -> 进入权限列表
        if (isAdSource && !CommonPermissions.isGranted(iView.getAct(), Permission.READ_SMS)){
            iView.showTiyendeLimitPermissionPage()
            return
        }

        //已同意权限列表 && 已同意SMS权限 -> 上报设备数据
        if (permissionStatus == 1 && CommonPermissions.isGranted(iView.getAct(), Manifest.permission.READ_SMS)){
            //上报设备数据
            TiyendeUserDecInvok.queryTiyendeDeviceInfoTypesAction()
        }
        showTiyendeLimitReviewingPermissionSuccess()
    }

    /**
     * 权限检查成功
     */
    private fun showTiyendeLimitReviewingPermissionSuccess(){
        iView.showTiyendeLimitApplyPage()
    }
}